package com.example.blog.modules.system.service.impl;

import com.example.blog.common.exception.ServiceException;
import com.example.blog.modules.system.service.CacheWarmUpService;
import com.example.blog.modules.system.service.DashboardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
@RequiredArgsConstructor
public class CacheWarmUpServiceImpl implements CacheWarmUpService {

    private final DashboardService dashboardService;
    private final ExecutorService executorService = Executors.newFixedThreadPool(3);

    @Override
    public void warmUpCache(String cacheName) {
        if ("dashboard".equals(cacheName)) {
            warmUpDashboardCache();
        } else {
            throw new ServiceException("不支持的缓存预热类型：" + cacheName);
        }
    }

    private void warmUpDashboardCache() {
        log.info("开始预热仪表盘缓存...");
        LocalDate today = LocalDate.now();
        List<CompletableFuture<Void>> futures = Arrays.asList(
            // 预热今日数据
            CompletableFuture.runAsync(() -> {
                try {
                    dashboardService.getDashboardData(today, today);
                    log.info("今日数据缓存预热完成");
                } catch (Exception e) {
                    log.error("今日数据缓存预热失败", e);
                }
            }, executorService),

            // 预热最近7天数据
            CompletableFuture.runAsync(() -> {
                try {
                    dashboardService.getTrendData(7, null);
                    log.info("最近7天趋势数据缓存预热完成");
                } catch (Exception e) {
                    log.error("最近7天趋势数据缓存预热失败", e);
                }
            }, executorService),

            // 预热最近30天数据
            CompletableFuture.runAsync(() -> {
                try {
                    dashboardService.getTrendData(30, null);
                    log.info("最近30天趋势数据缓存预热完成");
                } catch (Exception e) {
                    log.error("最近30天趋势数据缓存预热失败", e);
                }
            }, executorService),

            // 预热热门分类数据
            CompletableFuture.runAsync(() -> {
                try {
                    dashboardService.getTopCategories(10);
                    log.info("热门分类数据缓存预热完成");
                } catch (Exception e) {
                    log.error("热门分类数据缓存预热失败", e);
                }
            }, executorService),

            // 预热热门标签数据
            CompletableFuture.runAsync(() -> {
                try {
                    dashboardService.getTopTags(10);
                    log.info("热门标签数据缓存预热完成");
                } catch (Exception e) {
                    log.error("热门标签数据缓存预热失败", e);
                }
            }, executorService)
        );

        // 等待所有预热任务完成
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        log.info("仪表盘缓存预热完成");
    }
} 